package com.assumejson.mybatis.config;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.mybatis.spring.mapper.MapperScannerConfigurer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

import java.io.IOException;
import java.io.InputStream;

/**
 * @author Mr. Li
 * @version 1.0
 * @since 1.0 2021/12/8 13:35
 */
@Configuration
@ComponentScan("com.assumejson.mybatis")
//@MyMapperScan("com.assumejson.mybatis.mapper")
//@MapperScan
public class AppConfig {

	@Bean
	public SqlSessionFactory sqlSessionFactory() throws IOException {
		InputStream resourceAsStream = Resources.getResourceAsStream("mybatis.xml");
		return new SqlSessionFactoryBuilder().build(resourceAsStream);
	}

	@Bean
	public MapperScannerConfigurer mapperScannerConfigurer() {
		MapperScannerConfigurer mapperScannerConfigurer = new MapperScannerConfigurer();
		mapperScannerConfigurer.setBasePackage("com.assumejson.mybatis.mapper");

		return mapperScannerConfigurer;
	}
}
